package org.xy.parser;

import java.util.Arrays;
import java.util.Map;

import org.xy.bean.Bean;
import org.xy.bean.BeanWapper;

public class BeanDefinitionSingletonParser extends RootParser {

	public BeanDefinitionSingletonParser(Map<String, Bean> beans) {
		super(beans);
	}

	@Override
	protected BeanWapper getBeanWapper(String beanName) {
		if (this.exist(beanName)) {
			this.logger.info("Exist BeanWapper[" + beanName
					+ "] in map, return it directly");
			return beaWappers.get(beanName);
		}

		Bean current = this.beans.get(beanName);
		BeanWapper beanWapper = new BeanWapper(current);
		if (current.hasChild()) {
			this.logger.info("BeanWapper[" + beanName + "] has child"
					+ Arrays.toString(current.getChildNames().toArray()));
			for (String childName : current.getChildNames()) {
				this.logger.info("get child bean[" + childName + "]");
				BeanWapper child = this.getBeanWapper(childName);
				beanWapper.addChild(child);
				beaWappers.put(childName, child);
				this.logger.info("put BeanWapper[" + childName + "] into map");
			}
			beaWappers.put(beanName, beanWapper);
			this.logger.info("put BeanWapper[" + beanWapper + "] into map");
		}
		return beanWapper;
	}

//	@Override
//	public Map<String, BeanWapper> getBeanWappers() {
//		if (this.beans == null || this.beans.isEmpty()) {
//			return null;
//		}
//
//		for (String beanName : this.beans.keySet()) {
//			this.logger.info("get BeanWapper[" + beanName + "]");
//
//			BeanWapper beanWapper = this.getBeanWapper(beanName);
//			this.logger.info("BeanWapper[" + beanName + "] have ["
//					+ beanWapper.childSize() + "] childs");
//			this.logger.info("");
//		}
//
//		return beaWappers;
//	}
}
